package Decoration;

import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import java.io.IOException;

public class PriceByDecorationMapper extends Mapper<LongWritable, Text, Text, DoubleWritable> {

    private static final int DECORATION_INDEX = 10;
    private static final int PRICE_INDEX = 14;

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        String line= value.toString();
        if (line.isEmpty()) return;

        String[] fields = line.split(",");

        if (fields.length > Math.max(DECORATION_INDEX, PRICE_INDEX)) {
            String decoration = fields[DECORATION_INDEX].trim();
            try {
                double pricePerSquareMeter = Double.parseDouble(fields[PRICE_INDEX].trim());
                context.write(new Text(decoration), new DoubleWritable(pricePerSquareMeter));
                System.out.println("Mapped: " + decoration + " -> " + pricePerSquareMeter);
            } catch (NumberFormatException e) {
                System.err.println("解析数据时出错，位于行数为: " + line);
            }
        }
    }
}